package org.example.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.admin.dto.CategoryTree;
import org.example.admin.dto.CategoryTreeChildren;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.CategoryFirst;
import org.example.db.entity.CategorySecond;
import org.example.db.mapper.CategoryFirstMapper;
import org.example.db.mapper.CategorySecondMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class AdminGoodsCategoryService {
    CategoryFirstMapper categoryFirstMapper;
    CategorySecondMapper categorySecondMapper;

    public AdminGoodsCategoryService(CategoryFirstMapper categoryFirstMapper, CategorySecondMapper categorySecondMapper) {
        this.categoryFirstMapper = categoryFirstMapper;
        this.categorySecondMapper = categorySecondMapper;
    }


    public List<CategoryFirst> getCategoryList(){
        List<CategoryFirst> categoryFirsts = new LinkedList<CategoryFirst>();
        categoryFirsts = categoryFirstMapper.selectList(null);
        return categoryFirsts;
    }
    public List<CategorySecond> getCategoryList2(){
        List<CategorySecond> categorySeconds = new LinkedList<CategorySecond>();
        categorySeconds = categorySecondMapper.selectList(null);
        return categorySeconds;
    }

    public void deleteCategory(Long id){
        categoryFirstMapper.deleteById(id);
    }

    public void deleteCategory2(Long id){
        categorySecondMapper.deleteById(id);
    }

    public void deleteCategory2s(Long parentId){
        categorySecondMapper.delete(new QueryWrapper<CategorySecond>().lambda().eq(CategorySecond::getParentId,parentId));
    }

    public int updateCy1(CategoryFirst categoryFirst){
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        categoryFirst.setUpdateTime(time);
        return categoryFirstMapper.updateById(categoryFirst);
    }

    public int createCy1(CategoryFirst categoryFirst){
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        categoryFirst.setAddTime(time);
        categoryFirst.setUpdateTime(time);
        return categoryFirstMapper.insert(categoryFirst);
    }

    public int updateCy2(CategorySecond categorySecond){
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        categorySecond.setUpdateTime(time);
        return categorySecondMapper.updateById(categorySecond);
    }

    public int createCy2(CategorySecond categorySecond){
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        categorySecond.setAddTime(time);
        categorySecond.setUpdateTime(time);
        return categorySecondMapper.insert(categorySecond);
    }


    public List<CategoryTree> listTree(){

        List<CategoryFirst> categoryFirstList = categoryFirstMapper.selectList(null);
        ArrayList<CategoryTree> categoryTreeList = new ArrayList<>();
        for(CategoryFirst categoryFirst: categoryFirstList){

            CategoryTree categoryTree = new CategoryTree();
            categoryTree.setLabel(categoryFirst.getName());
            categoryTree.setValue(String.valueOf(categoryFirst.getId()));
            List<CategorySecond> categorySecondList = categorySecondMapper.selectList(Wrappers.lambdaQuery(CategorySecond.class).eq(CategorySecond::getParentId, categoryFirst.getId()));
            List<CategoryTreeChildren> categoryTreeChildrenList = new ArrayList<>();
            for(CategorySecond categorySecond: categorySecondList){

                CategoryTreeChildren categoryTreeChildren = new CategoryTreeChildren();
                categoryTreeChildren.setLabel(categorySecond.getName());
                categoryTreeChildren.setValue(String.valueOf(categorySecond.getId()));
                categoryTreeChildrenList.add(categoryTreeChildren);
            }
            categoryTree.setChildren(categoryTreeChildrenList);
            categoryTreeList.add(categoryTree);
        }

        return categoryTreeList;
    }

}
